package com.springboot_shopping.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.springboot_shopping.entity.UserAddress;
import com.springboot_shopping.Mapper.UserAddressMapper;
import com.springboot_shopping.service.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class UserAddressServiceImpl implements UserAddressService {

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Override
    public UserAddress findById(Integer id) {
        return userAddressMapper.selectById(id);
    }

    @Override
    public List<UserAddress> findByUserId(Integer userId) {
        return userAddressMapper.findByUserId(userId);
    }

    @Override
    public List<UserAddress> findByLabelAndUserId(String label, Integer userId) {
        return userAddressMapper.findByLabelAndUserId(label, userId);
    }

    @Override
    public boolean setDefaultAddress(Integer id, Integer userId) {
        // 取消其他地址的默认状态
        cancelOtherDefault(userId);

        // 设置当前地址为默认
        return userAddressMapper.setDefaultAddress(id, userId) > 0;
    }

    // 取消用户其他地址的默认状态
    private void cancelOtherDefault(Integer userId) {
        userAddressMapper.cancelOtherDefault(userId);
    }

    @Override
    public boolean addAddress(UserAddress address) {
        Date now = new Date();
        address.setCreateTime(now);
        address.setUpdateTime(now);

        // 如果是默认地址，需要取消其他地址的默认状态
        if (address.getIsDefault() == 1) {
            cancelOtherDefault(address.getUserId());
        }

        return userAddressMapper.insert(address) > 0;
    }

    @Override
    public boolean updateAddress(UserAddress address) {
        address.setUpdateTime(new Date());

        // 如果修改为默认地址，需要取消其他地址的默认状态
        if (address.getIsDefault() == 1) {
            cancelOtherDefault(address.getUserId());
        }

        return userAddressMapper.updateById(address) > 0;
    }

    @Override
    public boolean deleteAddress(Integer id, Integer userId) {
        // 检查地址是否属于该用户
        QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id).eq("user_id", userId);
        UserAddress address = userAddressMapper.selectOne(queryWrapper);

        if (address == null) {
            return false;
        }

        // 如果删除的是默认地址，需要将第一个地址设为默认
        if (address.getIsDefault() == 1) {
            QueryWrapper<UserAddress> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id", userId).ne("id", id).orderByAsc("id");
            List<UserAddress> addressList = userAddressMapper.selectList(wrapper);

            if (!addressList.isEmpty()) {
                UserAddress firstAddress = addressList.get(0);
                firstAddress.setIsDefault(1);
                userAddressMapper.updateById(firstAddress);
            }
        }

        return userAddressMapper.deleteById(id) > 0;
    }


}